/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						  scenario_cut_scene_info.cpp

	$Header: /game/scenario_cut_scene_info.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "scenario_cut_scene_info.h"

#include <assert.h>

#include "enum_operations.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
t_scenario_cut_scene_info::t_image_id const t_scenario_cut_scene_info::k_image_none =
	k_scenario_cut_scene_image_none;

t_scenario_cut_scene_info::t_voice_over_id const t_scenario_cut_scene_info::k_voice_over_none =
	k_scenario_cut_scene_voice_over_none;

// --------------------------------------------------------------------------
// Unnamed namespace
// --------------------------------------------------------------------------

namespace
{
	int const k_current_format_version = 0;
	int const k_current_map_format_version = 0;

	// --------------------------------------------------------------------------
	// --------------------------------------------------------------------------
	typedef t_scenario_cut_scene_info::t_image_id		t_image_id;
	typedef t_scenario_cut_scene_info::t_voice_over_id	t_voice_over_id;

	t_image_id const k_image_count = t_image_id( t_scenario_cut_scene_info::k_image_count );
	t_image_id const k_image_none = t_scenario_cut_scene_info::k_image_none;
	t_voice_over_id const k_voice_over_count = t_voice_over_id( t_scenario_cut_scene_info::k_voice_over_count );
	t_voice_over_id const k_voice_over_none = t_scenario_cut_scene_info::k_voice_over_none;

	// --------------------------------------------------------------------------
	// --------------------------------------------------------------------------
	template < typename t_id, int k_count >
	bool read_id( std::streambuf & stream, t_id none_value, t_id & result )
	{
		std::string keyword;
		if ( !read_string16( stream, keyword ) )
			return false;

		if ( keyword.empty() )
		{
			result = none_value;
			return true;
		}

		t_id id;
		for	( id = t_id( 0 ); true; enum_incr( id ) )
		{
			if ( id >= k_count )
				return false;

			if ( get_keyword( id ) == keyword )
				break;
		}

		result = id;
		return true;
	}

	// --------------------------------------------------------------------------
	// --------------------------------------------------------------------------
	bool read_image_id( std::streambuf & stream, t_image_id & result )
	{
		return read_id< t_image_id, k_image_count >( stream, k_image_none, result );
	}

	// --------------------------------------------------------------------------
	// --------------------------------------------------------------------------
	bool read_voice_over_id( std::streambuf & stream, t_voice_over_id & result )
	{
		return read_id< t_voice_over_id, k_voice_over_count >( stream, k_voice_over_none, result );
	}

}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_scenario_cut_scene_info::read( std::streambuf & stream )
{
	int format_version = get< t_uint16 >( stream );
	if ( format_version < 0 || format_version > k_current_format_version )
		return false;

	return		read_string16( stream, m_text )
			&&	read_image_id( stream, m_image_id )
			&&	read_voice_over_id( stream, m_voice_over_id );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_scenario_cut_scene_info::read_from_map( std::streambuf & stream )
{
	int format_version = get< t_uint16 >( stream );
	if ( format_version < 0 || format_version > k_current_map_format_version )
		return false;

	return		read_string16( stream, m_text )
			&&	read_image_id( stream, m_image_id )
			&&	read_voice_over_id( stream, m_voice_over_id );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_scenario_cut_scene_info::write( std::streambuf & stream ) const
{
	put< t_uint16 >( stream, k_current_format_version );

	return		write_string16( stream, m_text )
			&&	write_string16( stream, get_keyword( m_image_id ) )
			&&	write_string16( stream, get_keyword( m_voice_over_id ) );
}
